package com.tjp.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.mysql.cj.util.StringUtils;
import com.tjp.subject.application.convert.SubjectCategoryBOConverter;
import com.tjp.subject.application.entity.SubjectCategoryDTO;
import com.tjp.subject.common.entity.JZResult;
import com.tjp.subject.common.entity.PageResult;
import com.tjp.subject.common.enums.IsDeletedFlagEnum;
import com.tjp.subject.domain.entity.SubjectCategoryBO;
import com.tjp.subject.domain.service.SubjectCategoryDomainService;
import com.tjp.subject.infra.basic.entity.SubjectCategory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: jz-club
 * @description: 题目分类控制器
 * @author: aaronTang
 * @create: 2024-02-22
 */
@RestController
@RequestMapping("/subject/category")
@Slf4j
public class SubjectCategoryController {

    @Autowired
    private SubjectCategoryDomainService subjectCategoryDomainService;

    @PostMapping("/add")
    public JZResult<Boolean> insert(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("subjectCategoryDTO is {}", JSON.toJSONString(subjectCategoryDTO));
            }

            Preconditions.checkNotNull(subjectCategoryDTO.getCategoryType(), "分类类型不能为空");
            // 确保 categoryName 不为空
            Preconditions.checkArgument(!StringUtils.isNullOrEmpty(subjectCategoryDTO.getCategoryName()), "分类名称不能为空");

            SubjectCategoryBO subjectCategoryBO = SubjectCategoryBOConverter.INSTANCE.DTOToBO(subjectCategoryDTO);
            subjectCategoryDomainService.insert(subjectCategoryBO);
            return JZResult.ok();
        }catch (Exception e) {
            log.error("SubjectCategoryController.insert.error: {}", e.getMessage(), e);
            return JZResult.fail();
        }
    }

    @PostMapping("/queryPrimaryCategory")
    public JZResult<List<SubjectCategoryDTO>> queryPrimaryCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryBOConverter.INSTANCE.convertDTOToCategoryBO(subjectCategoryDTO);
            List<SubjectCategoryBO> subjectCategoryBOList = subjectCategoryDomainService.queryCategory(subjectCategoryBO);

            List<SubjectCategoryDTO> subjectCategoryDTOList = SubjectCategoryBOConverter.INSTANCE.BOListToDTOList(subjectCategoryBOList);
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.subjectCategoryDTOList is {}", JSON.toJSONString(subjectCategoryDTOList));
            }
            return JZResult.ok(subjectCategoryDTOList);
        }catch (Exception e) {
            log.error("SubjectCategoryController.queryPrimaryCategory.error: {}", e.getMessage(), e);
            return JZResult.fail();
        }
    }

    @PostMapping("/queryCategoryByPrimary")
    public JZResult<List<SubjectCategoryDTO>> queryCategoryByPrimary(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.queryCategoryByPrimary.SubjectCategoryDTO is {}", JSON.toJSONString(subjectCategoryDTO));
            }

            Preconditions.checkNotNull(subjectCategoryDTO.getParentId(), "父类id不能为空");

            SubjectCategoryBO subjectCategoryBO = SubjectCategoryBOConverter.INSTANCE.convertDTOToCategoryBO(subjectCategoryDTO);

            List<SubjectCategoryBO> subjectCategoryBOList = subjectCategoryDomainService.queryCategory(subjectCategoryBO);

            List<SubjectCategoryDTO> subjectCategoryDTOList = SubjectCategoryBOConverter.INSTANCE.BOListToDTOList(subjectCategoryBOList);
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.subjectCategoryDTOList is {}", JSON.toJSONString(subjectCategoryDTOList));
            }
            return JZResult.ok(subjectCategoryDTOList);
        }catch (Exception e) {
            log.error("SubjectCategoryController.queryPrimaryCategory.error: {}", e.getMessage(), e);
            return JZResult.fail();
        }
    }


    /**
     * 更新分类
     * @param subjectCategoryDTO
     * @return
     */
    @PostMapping("/update")
    public JZResult<Boolean> update(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.update.subjectCategoryDTO is {}", JSON.toJSONString(subjectCategoryDTO));
            }

            SubjectCategoryBO subjectCategoryBO = SubjectCategoryBOConverter.INSTANCE.convertDTOToCategoryBO(subjectCategoryDTO);


            int result = subjectCategoryDomainService.update(subjectCategoryBO);

            return JZResult.ok(result);
        }catch (Exception e) {
            log.error("SubjectCategoryController.queryPrimaryCategory.error: {}", e.getMessage(), e);
            return JZResult.fail("更新分类失败");
        }
    }


    /**
     * 删除分类
     * @param subjectCategoryDTO
     * @return
     */
    @PostMapping("/delete")
    public JZResult<Boolean> delete(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.delete.subjectCategoryDTO is {}", JSON.toJSONString(subjectCategoryDTO));
            }

            SubjectCategoryBO subjectCategoryBO = SubjectCategoryBOConverter.INSTANCE.convertDTOToCategoryBO(subjectCategoryDTO);

            int result = subjectCategoryDomainService.delete(subjectCategoryBO);

            return JZResult.ok(result);
        }catch (Exception e) {
            log.error("SubjectCategoryController.queryPrimaryCategory.delete: {}", e.getMessage(), e);
            return JZResult.fail("删除分类失败");
        }
    }



}
